/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.storage;

import org.apache.axis2.context.*;
import org.apache.axis2.description.*;
import org.apache.log4j.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

import java.net.*;

/**
 * Provide connection to the database through Hibernate
 */
public class DataBaseManager
{
    private static final Logger logger = Logger.getLogger (DataBaseManager.class);
    private static SessionFactory sessionFactory = null;

    static
    {
        logger.info ("Opening a database connection");

        try
        {
            // Create session factory
            AxisService axisService = MessageContext.getCurrentMessageContext ().getAxisService ();
            ClassLoader serviceClassLoader = axisService.getClassLoader ();
            URL configURL = serviceClassLoader.getResource ("hibernate.cfg.xml");
            sessionFactory = new AnnotationConfiguration ().configure (configURL).buildSessionFactory ();
        }
        catch (HibernateException ex)
        {
            logger.error ("Error opening database connection", ex);
        }
    }

    /**
     * Session factory getter method
     *
     * @return SessionFactory
     */
    public static SessionFactory getSessionFactory ()
    {
        return sessionFactory;
    }
}
